package com.gary.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gary.demo.biz.GoodsBO;
import com.gary.demo.entity.Goods;
import com.gary.demo.model.param.GoodsOperateParam;
import com.gary.demo.model.param.GoodsQueryParam;
import com.gary.demo.vo.GoodsVO;
import com.gary.demo.vo.param.GoodsOperateParamVO;
import com.gary.demo.vo.param.GoodsQueryParamVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsBO goodsBO;

    @PostMapping("/add")
    public Integer add(@RequestBody GoodsOperateParamVO paramVO) {
        GoodsOperateParam param = new GoodsOperateParam();
        BeanUtils.copyProperties(paramVO, param);
        return goodsBO.add(param);
    }

    @PostMapping("/update")
    public void update(@RequestBody GoodsOperateParamVO paramVO) {
        GoodsOperateParam param = new GoodsOperateParam();
        BeanUtils.copyProperties(paramVO, param);
        goodsBO.update(param);
    }


    @GetMapping("/pageQuery")
    public IPage<GoodsVO> pageQuery(GoodsQueryParamVO paramVO) {
        GoodsQueryParam param = new GoodsQueryParam();
        BeanUtils.copyProperties(paramVO, param);
        IPage<Goods> goodsIPage = goodsBO.pageQuery(param);
        List<GoodsVO> goodsVOList = goodsIPage.getRecords().stream().map(item -> {
            GoodsVO goodsVO = new GoodsVO();
            BeanUtils.copyProperties(item, goodsVO);
            return goodsVO;
        }).collect(Collectors.toList());
        Page<GoodsVO> goodsVOPage = new Page<>();
        goodsVOPage.setRecords(goodsVOList);
        goodsVOPage.setCurrent(goodsIPage.getCurrent());
        goodsVOPage.setPages(goodsIPage.getPages());
        goodsVOPage.setTotal(goodsIPage.getTotal());
        goodsVOPage.setSize(goodsIPage.getSize());
        return goodsVOPage;
    }

}
